package com.horoscope.astrology.zodiac.signs.free.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.CityInfo

class CityAdapter(private val mContext: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val mData = ArrayList<CityInfo>()


    fun setData(list: ArrayList<CityInfo>) {
        mData.clear()
        mData.addAll(list)
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_select_city, parent, false)
        return CityHolder(view)

    }

    override fun getItemCount(): Int {
        return mData.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is CityHolder -> {
                val bean = mData[position]
                holder.city.text = "${bean.name}, ${bean.adminName1}, ${bean.countryName}"
                holder.city.setOnClickListener {
                    mListener?.onCityClick(bean)
                }
            }
        }
    }

    inner class CityHolder(item: View) : RecyclerView.ViewHolder(item) {

        val city = item.findViewById<TextView>(R.id.text_view_city)
    }

    private var mListener: ICityOnClick? = null
    fun setClickListener(listener: ICityOnClick) {
        mListener = listener
    }

    interface ICityOnClick {
        fun onCityClick(bean: CityInfo)
    }
}